package slicepackage

import (
	"fmt"
	"sort"
)

func test1() {
	var arr1 [5]int
	for i := 0; i < len(arr1); i++ {
		fmt.Println(i, arr1[i])
	}
}

func test2() {
	var arr1 [5]int
	for i, j := range arr1 {
		fmt.Println(i, j)
	}
}

func test3() {
	a := [...]string{"a", "b", "c", "d"}
	for i := range a {
		fmt.Println("Array item", i, "is", a[i])
	}
}

func t4func1(arr [5]int) {
	fmt.Println("t4fun1before", arr)
	for i := 0; i < len(arr); i++ {
		arr[i] += 3
	}
	fmt.Println("t4func1after", arr)
}

func t4func2(arr *[5]int) {
	fmt.Println("t4fun2before", arr)
	for i := 0; i < len(arr); i++ {
		arr[i] += 3
	}
	fmt.Println("t4func2after", arr)
}

func test4() {
	var arr1 [5]int
	t4func1(arr1)
	fmt.Println(arr1)

	t4func2(&arr1)
	fmt.Println(arr1)
}

func test5() {
	arr1 := [5]int{1, 2, 3}
	arr2 := [5]int{3: 2, 4: 2}

	fmt.Println(arr1)
	fmt.Println(arr2)
}

func test6() {
	arr1 := [6]int{0, 1, 2, 3, 4, 5}

	// 生成的切片是arr1中，下标[2, 4)对应的内容，也就是2 3
	s1 := arr1[2:4]
	fmt.Println(s1)

	// 还可以这样生成，下标[3, 6)对应的内容，也就是3 4 5
	s2 := arr1[3:]
	fmt.Println(s2)

	// 还可以这样生成，下标[0, 4)对应的内容，也就是0 1 2 3
	s3 := arr1[:4]
	fmt.Println(s3)

}

func test7() {
	b := []byte{'g', 'o', 'l', 'a', 'n', 'g'}
	fmt.Println(b[1:4])
	fmt.Println(b[2:])
	fmt.Println(b[:])
}

func sum(a []int) int {
	res := 0
	for _, v := range a {
		res += v
	}
	return res
}

func test8Func(a []int) {
	for i, _ := range a {
		a[i] = 0
	}
}

func test8() {
	a := [5]int{1, 2, 3, 4, 5}
	fmt.Println(sum(a[:]))
	fmt.Println(sum(a[1:]))

	test8Func(a[3:])
	fmt.Println(a)
}

func test9() {
	var s1 []int
	fmt.Println("s1 len: ", len(s1))
	s2 := make([]int, 100)
	fmt.Println("s2 len: ", len(s2))
}

func test10() {
	s1 := make([]int, 0, 10)
	fmt.Println("s1的大小：", len(s1))
	fmt.Println("s1的容量：", cap(s1))

	s1 = s1[0 : len(s1)+1]
	fmt.Println("1扩容后s1的大小：", len(s1))
	fmt.Println("1扩容后s1的容量：", cap(s1))

	s1 = s1[0:cap(s1)]
	fmt.Println("2扩容后s1的大小：", len(s1))
	fmt.Println("2扩容后s1的容量：", cap(s1))

	s1 = s1[5:cap(s1)]
	fmt.Println("3扩容后s1的大小：", len(s1))
	fmt.Println("3扩容后s1的容量：", cap(s1))

	s1 = s1[2:cap(s1)]
	fmt.Println("4扩容后s1的大小：", len(s1))
	fmt.Println("4扩容后s1的容量：", cap(s1))

	s1 = s1[2:20]
	fmt.Println("5扩容后s1的大小：", len(s1))
	fmt.Println("5扩容后s1的容量：", cap(s1))
}

func test11() {
	slFrom := []int{1, 2, 3}
	slTo := make([]int, 10)

	// copy
	n := copy(slTo, slFrom)
	fmt.Println("n: ", n, " slTo: ", slTo)

	slTo = append(slTo, 7, 8, 9)

	fmt.Println(slTo)
}

func test12() {
	s := "hello go"
	c := []byte(s)
	fmt.Println(s)
	fmt.Println(c)
	fmt.Printf("%s", c)
}

func test13() {
	s := "hello go"
	c := make([]byte, len(s))
	copy(c, s)
	fmt.Println(s)
	fmt.Printf("%s", c)
}

func test14() {
	s := "hello go"
	fmt.Println("[2, 3)", s[2:3])
	fmt.Println("[0, 5)", s[:5])
	fmt.Println("[2, +)", s[2:])
}

func test15() {
	s := "hello"
	//s[0] = 'c'  错误写法
	c := []byte(s)
	c[0] = 'c'
	s2 := string(c)
	fmt.Println(s2)
}

func test16CheckSort(s []int) {
	fmt.Println(s)
	if sort.IntsAreSorted(s) {
		fmt.Println("切片已有序")
	} else {
		fmt.Println("切片无序")
	}
}

func test16() {
	// 排序整数元素
	s1 := []int{1, 2, 3, 6, 4, 3}
	test16CheckSort(s1)
	sort.Ints(s1)
	test16CheckSort(s1)

	fmt.Println("-------------------------------")

	// 排序字符串元素
	s2 := []string{"hello", "go", "and", "cpp"}
	fmt.Println(s2)
	sort.Strings(s2)
	fmt.Println(s2)
}

func test17() {
	s1 := []int{1, 2, 10, 200, 3, 5}
	sort.Ints(s1)
	fmt.Println(sort.SearchInts(s1, 11))
	fmt.Println(sort.SearchInts(s1, 1))
}

func Test() {
	test17()
}
